package com.itboye.mengwu.main;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseFragment;
import com.itboye.core.object.ResultEntity;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.ui.main.ActivityGlobalShop;
import com.itboye.mengwu.app.ui.mymeng.MessageActivity;
import com.itboye.mengwu.app.ui.roof.ActivityHomePageSearch;
import com.itboye.mengwu.app.ui.roof.HomPageItemFragment;
import com.itboye.mengwu.app.ui.roof.HomePageItemFragmentTypeTwo;
import com.itboye.mengwu.data.model.AppHeadPage;
import com.itboye.mengwu.data.presenter.HomePagePresenter;
import com.itboye.mengwu.ui.shop.FlowLayout;
import com.itboye.mengwu.widget.PagerSlidingTabStrip;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/***
 * 屋顶Fragment
 * 
 * @author dsawe
 * 
 */
public class HomeFragment extends BaseFragment implements Observer {
	String[] titles;
	List<String> biaoqian1 = new ArrayList<String>();
	List<String> golobalPageID = new ArrayList<String>();
	List<String> biaoqian2 = new ArrayList<String>();
	List<String> otherPageID = new ArrayList<String>();
	ViewPager pager;
	PagerSlidingTabStrip tabs;
	boolean isShow = false;
	TextView txt_more;
	private ImageView img_other, back;
	private ImageView img_news;
	boolean readed = false;
	// LunBoPresenter boPresenter;
	HomePagePresenter homePagePresenter;
	protected PopupWindow popupWindow;
	static boolean isreturn;
	AppHeadPage arrayBanner;
	ArrayList<AppHeadPage> arrayList = new ArrayList<AppHeadPage>();
	LinearLayout nonet, normal;
	ArrayList<String> pageID = new ArrayList<>();

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.fragment_roof;
	}

	@Override
	public void onMyActivityCreated() {
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(),
						ActivityHomePageSearch.class);
				startActivity(intent);
			}
		});
		IntentFilter filter = new IntentFilter("change_img");
		getActivity().registerReceiver(receiver, filter);
		IntentFilter readed = new IntentFilter("readed");
		getActivity().registerReceiver(gonereceiver, readed);

		img_other.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				// LoginController.QuanMessge(getActivity(), null);
				Intent intent = new Intent(getActivity(), MessageActivity.class);
				startActivity(intent);

			}
		});
		img_news.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(), MessageActivity.class);
				startActivity(intent);
//				LoginController.QuanMessge(getActivity(), null);

			}
		});
		txt_more.setOnClickListener(new OnClickListener() {

			@SuppressWarnings("deprecation")
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (!isShow) {
					View view = LayoutInflater.from(getActivity()).inflate(
							R.layout.popupwindow_more, null);
					popupWindow = new PopupWindow(getActivity()
							.getWindowManager().getDefaultDisplay().getWidth(),
							getActivity().getWindowManager()
									.getDefaultDisplay().getHeight() / 3);
					popupWindow.setOutsideTouchable(true);
					popupWindow.setFocusable(true);
					// popupWindow.setBackgroundDrawable(new BitmapDrawable(
					// BitmapFactory.decodeResource(getResources(),
					// R.drawable.bg_category_normal)));
					// popupWindow.setBackgroundDrawable(new BitmapDrawable());
					ColorDrawable dw = new ColorDrawable(0xD4F1F6F6);
					popupWindow.setBackgroundDrawable(dw);
					popupWindow.setContentView(view);
					TextView other = (TextView) view.findViewById(R.id.other);
					FlowLayout biaoqian_1 = (FlowLayout) view
							.findViewById(R.id.biaoqian_1);
					for (int i = 0; i < biaoqian1.size(); i++) {
						TextView textView = new TextView(getActivity());
						// textView.setText(HomeFragment.this.biaoqian_1[i]);
						textView.setText(biaoqian1.get(i));
						textView.setBackground(getResources().getDrawable(
								R.drawable.biaoqianshape));
						android.widget.LinearLayout.LayoutParams layoutParams = new android.widget.LinearLayout.LayoutParams(
								android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,
								android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
						layoutParams.setMargins(20, 10, 20, 10);
						biaoqian_1.addView(textView, layoutParams);
						textView.setTag(i);
						textView.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View v) {
								// TODO Auto-generated method stub
								Intent intent = new Intent(getActivity(),
										ActivityGlobalShop.class);
								Log.e("hu",
										golobalPageID.get(Integer.parseInt(v
												.getTag() + "")));
								intent.putExtra(
										"page_name",
										biaoqian1.get(Integer.parseInt(v
												.getTag() + "")));
								intent.putExtra(
										"page_id",
										golobalPageID.get(Integer.parseInt(v
												.getTag() + "")));
								startActivity(intent);
							}
						});
					}

					FlowLayout biaoqian_2 = (FlowLayout) view
							.findViewById(R.id.biaoqian_2);
					if (biaoqian2.size() == 0) {
						other.setVisibility(View.GONE);
					} else {
						for (int i = 0; i < biaoqian2.size(); i++) {
							TextView textView = new TextView(getActivity());
							textView.setText(biaoqian2.get(i));
							textView.setBackground(getResources().getDrawable(
									R.drawable.biaoqianshape));
							android.widget.LinearLayout.LayoutParams layoutParams = new android.widget.LinearLayout.LayoutParams(
									android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,
									android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
							layoutParams.setMargins(20, 10, 20, 10);
							biaoqian_2.addView(textView, layoutParams);
							textView.setTag(i);
							textView.setOnClickListener(new OnClickListener() {

								@Override
								public void onClick(View v) {
									// TODO Auto-generated method stub
									Intent intent = new Intent(getActivity(),
											ActivityGlobalShop.class);
									Log.e("hu",
											otherPageID.get(Integer.parseInt(v
													.getTag() + "")));
									intent.putExtra(
											"page_name",
											biaoqian2.get(Integer.parseInt(v
													.getTag() + "")));
									intent.putExtra(
											"page_id",
											otherPageID.get(Integer.parseInt(v
													.getTag() + "")));
									startActivity(intent);
								}
							});
						}
					}

					popupWindow.showAsDropDown(v);
					txt_more.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.up));
				} else {
					txt_more.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.down));
					popupWindow.dismiss();

				}
				isShow = !isShow;
				popupWindow.setOnDismissListener(new OnDismissListener() {

					@SuppressWarnings("deprecation")
					@Override
					public void onDismiss() {
						// TODO Auto-generated method stub
						txt_more.setBackgroundDrawable(getResources()
								.getDrawable(R.drawable.down));
						isShow = false;
					}
				});
			}
		});

		homePagePresenter = new HomePagePresenter(this);
		AppHeadPage headPage = new AppHeadPage();
		homePagePresenter.getInfo(headPage);

	}

	BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			visiable();
		}

	};
	BroadcastReceiver gonereceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			Gone();
		}

	};

	void visiable() {
		img_news.setVisibility(View.VISIBLE);
		img_other.setVisibility(View.GONE);

	}

	void Gone() {
		img_news.setVisibility(View.GONE);
		img_other.setVisibility(View.VISIBLE);

	}

	// @SuppressWarnings("deprecation")
	// @Override
	// public boolean onTouchEvent(MotionEvent event) {
	//
	// // TODO Auto-generated method stub
	//
	// if (popupWindow != null && popupWindow.isShowing()) {
	//
	// popupWindow.dismiss();
	//
	// popupWindow = null;
	// Log.e("hu", "click5");
	// txt_more.setBackgroundDrawable(getResources().getDrawable(
	// R.drawable.down));
	//
	// }
	//
	// return super.onTouchEvent(event);
	//
	// }

	public class MyAdapter extends FragmentStatePagerAdapter {
		String[] _titles;

		public MyAdapter(FragmentManager fm, String[] titles) {
			super(fm);
			_titles = titles;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return _titles[position];
		}

		@Override
		public int getCount() {
			return _titles.length;
		}

		@Override
		public Fragment getItem(int position) {
			Fragment bfrag2 = null;
			if (position == 0) {
				bfrag2 = new HomPageItemFragment();
			} else {
				bfrag2 = new HomePageItemFragmentTypeTwo(
						Integer.parseInt(pageID.get(position)));
				Log.e("hu+pageid", Integer.parseInt(pageID.get(position)) + "");

			}
			return bfrag2;
		}

	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity model = super.handlerError(data);
		if (model != null) {
			tabs.setFillViewport(false);
			if (model.getEventType() == HomePagePresenter.HomeQuerySuccessEvent) {
				AppHeadPage arrayBanner = (AppHeadPage) model.getData();
				if (arrayBanner != null) {
					titles = new String[arrayBanner.getCate().size()];
					for (int i = 0; i < arrayBanner.getCate().size(); i++) {
						titles[i] = arrayBanner.getCate().get(i).getName();
						pageID.add(arrayBanner.getCate().get(i).getPage_id());
					}

					pager.setAdapter(new MyAdapter(getChildFragmentManager(),
							titles));
					tabs.setViewPager(pager);
					for (int i = 0; i < arrayBanner.getCate_global().size(); i++) {
						biaoqian1.add(arrayBanner.getCate_global().get(i)
								.getName());
						golobalPageID.add(arrayBanner.getCate_global().get(i)
								.getPage_id());
					}
					if (arrayBanner.getCate_other() != null) {

						for (int i = 0; i < arrayBanner.getCate_other().size(); i++) {
							biaoqian2.add(arrayBanner.getCate_other().get(i)
									.getName());
							otherPageID.add(arrayBanner.getCate_other().get(i)
									.getPage_id());
						}
					}
				}
			} else {
				ByAlert.alert("请求失败");
			}

		}
	}

}
